import pygame
import random
import sys
import os

# 打印当前工作目录
print(os.getcwd())

# 初始化Pygame
pygame.init()

# 设置屏幕大小
screen_width = 720
screen_height = 1280
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("鹰击长空-Eagles strike across the sky-优雅草央千澈")

# 加载图像



# 加载 BOSS 动画序列
boss_frames = []
for i in range(10):  # 假设有15帧，从boss00.png到boss09.png
    frame = pygame.image.load(f"static/png/boss{i:02d}.png").convert_alpha()
    frame = pygame.transform.scale(frame, (200, 200))  # 调整为200x200像素
    boss_frames.append(frame)

# 加载玩家动画序列
player_frames = []
for i in range(12):  # 假设有12帧，从player_100.png到player_111.png
    frame = pygame.image.load(f"static/png/player_{100 + i:03d}.png").convert_alpha()
    frame = pygame.transform.scale(frame, (150, 150))  # 调整为150x150像素
    player_frames.append(frame)

# 加载敌人动画序列
enemy_frames = []
for i in range(5):  # 假设有5帧，从enemy_10.png到enemy_14.png
    frame = pygame.image.load(f"static/png/enemy_{i:02d}.png").convert_alpha()
    frame = pygame.transform.scale(frame, (80, 80))  # 调整为80x80像素
    enemy_frames.append(frame)

# 加载子弹动画序列
bullet_frames = []
for i in range(6):  # 假设有6帧，从bullet0.png到bullet5.png
    frame = pygame.image.load(f"static/png/bullet{i}.png").convert_alpha()
    frame = pygame.transform.scale(frame, (50, 50))  # 调整为50x50像素
    bullet_frames.append(frame)

# 加载爆炸动画序列
explosion_frames = []
for i in range(15):  # 假设有15帧，从00到14
    frame = pygame.image.load(f"static/png/explosion{i:02d}.png").convert_alpha()
    explosion_frames.append(frame)

# 加载声音
bullet_sound = pygame.mixer.Sound("static/sounds/bullet.wav")

# 设置玩家初始位置
player_x = screen_width // 2
player_y = screen_height - 100
player_speed = 5

# 子弹组
bullets = pygame.sprite.Group()
bullet_speed = 10

# 敌人组
enemies = pygame.sprite.Group()
enemy_speed = 3

# 游戏循环标志
running = True

# 游戏时钟
clock = pygame.time.Clock()




# BOSS 类
class Boss(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.frames = [pygame.image.load(f"static/png/boss{i:02d}.png").convert_alpha() for i in range(10)]  # 调整为10帧
        self.frame = 0
        self.image = self.frames[self.frame]
        self.rect = self.image.get_rect(center=(screen_width // 2, -100))
        self.last_update = pygame.time.get_ticks()
        self.frame_rate = 100  # 每100毫秒切换一帧
        self.health = 100  # BOSS 的生命值
        self.move_direction = 1  # 1 表示向右移动，-1 表示向左移动
        self.move_speed = 2  # BOSS 的移动速度
        self.shoot_delay = 1000  # 发射子弹的间隔时间（毫秒）
        self.last_shot = pygame.time.get_ticks()  # 上一次发射子弹的时间

    def update(self):
        # 移动 BOSS
        self.rect.y += 2  # BOSS 向下移动
        if self.rect.top > 0:
            self.rect.x += self.move_speed * self.move_direction
            if self.rect.right > screen_width or self.rect.left < 0:
                self.move_direction *= -1  # 改变移动方向

        # 更新动画帧
        now = pygame.time.get_ticks()
        if now - self.last_update > self.frame_rate:
            self.last_update = now
            self.frame = (self.frame + 1) % len(self.frames)
            self.image = self.frames[self.frame]

        # 发射子弹
        if now - self.last_shot > self.shoot_delay:
            self.last_shot = now
            self.shoot()

    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.bottom)
        bullets.add(bullet)  # 使用 add 方法将子弹添加到组中

# 玩家类
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.frames = player_frames
        self.frame = 0
        self.image = self.frames[self.frame]
        self.rect = self.image.get_rect(center=(player_x, player_y))
        self.last_update = pygame.time.get_ticks()
        self.frame_rate = 100  # 每100毫秒切换一帧

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and self.rect.left > 0:
            self.rect.x -= player_speed
        if keys[pygame.K_RIGHT] and self.rect.right < screen_width:
            self.rect.x += player_speed

        # 更新动画帧
        now = pygame.time.get_ticks()
        if now - self.last_update > self.frame_rate:
            self.last_update = now
            self.frame = (self.frame + 1) % len(self.frames)
            self.image = self.frames[self.frame]

    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.top)
        bullets.add(bullet)  # 使用 add 方法将子弹添加到组中
        bullet_sound.play()

# 子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.frames = bullet_frames
        self.frame = 0
        self.image = self.frames[self.frame]
        self.rect = self.image.get_rect(center=(x, y))
        self.last_update = pygame.time.get_ticks()
        self.frame_rate = 100  # 每100毫秒切换一帧

    def update(self):
        self.rect.y -= bullet_speed
        if self.rect.bottom < 0:
            self.kill()

        # 更新动画帧
        now = pygame.time.get_ticks()
        if now - self.last_update > self.frame_rate:
            self.last_update = now
            self.frame = (self.frame + 1) % len(self.frames)
            self.image = self.frames[self.frame]

# 敌人类
class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.frames = enemy_frames
        self.frame = 0
        self.image = self.frames[self.frame]
        self.rect = self.image.get_rect(center=(random.randint(50, screen_width - 50), -50))
        self.last_update = pygame.time.get_ticks()
        self.frame_rate = 100  # 每100毫秒切换一帧

    def update(self):
        self.rect.y += enemy_speed
        if self.rect.top > screen_height:
            self.kill()

        # 更新动画帧
        now = pygame.time.get_ticks()
        if now - self.last_update > self.frame_rate:
            self.last_update = now
            self.frame = (self.frame + 1) % len(self.frames)
            self.image = self.frames[self.frame]

# 爆炸类
class Explosion(pygame.sprite.Sprite):
    def __init__(self, center):
        super().__init__()
        self.frames = explosion_frames
        self.frame = 0
        self.image = self.frames[self.frame]
        self.rect = self.image.get_rect(center=center)
        self.last_update = pygame.time.get_ticks()
        self.frame_rate = 50

    def update(self):
        now = pygame.time.get_ticks()
        if now - self.last_update > self.frame_rate:
            self.last_update = now
            self.frame += 1
            if self.frame == len(self.frames):
                self.kill()
            else:
                center = self.rect.center
                self.image = self.frames[self.frame]
                self.rect = self.image.get_rect(center=center)

# 创建玩家实例
player = Player()
all_sprites = pygame.sprite.Group(player)
all_sprites.add(bullets)  # 将子弹组添加到所有精灵组中
all_enemies = pygame.sprite.Group()

# 游戏主循环
boss_spawned = False
boss_spawn_time = 10000  # 10秒后生成BOSS

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.shoot()

    # 更新所有精灵
    all_sprites.update()
    all_enemies.update()

    # 生成敌人
    if random.randint(1, 100) < 5:
        enemy = Enemy()
        all_enemies.add(enemy)
        all_sprites.add(enemy)

    # 检测碰撞
    hits = pygame.sprite.groupcollide(all_enemies, bullets, True, True)
    for hit in hits:
        explosion = Explosion(hit.rect.center)
        all_sprites.add(explosion)

    # 检测 BOSS 是否生成
    if not boss_spawned and pygame.time.get_ticks() > boss_spawn_time:
        boss = Boss()
        all_sprites.add(boss)
        all_enemies.add(boss)
        boss_spawned = True
        pygame.mixer.music.load("static/sounds/boss.wav")  # 加载背景音乐
        pygame.mixer.music.play(-1)  # 播放背景音乐，-1表示循环播放

    # 绘制屏幕
    screen.fill((0, 0, 0))
    all_sprites.draw(screen)
    pygame.display.flip()

    # 控制帧率
    clock.tick(60)

# 退出游戏
pygame.quit()
sys.exit()